home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / pcl4c60.zip / PMSIMPLE.C < prev    next >
Text File  |  1996-10-20  |  4KB  |  169 lines

  1. /*
  2. **  pmsimple.c (10/27/95)
  3. **
  4. **  Protected Mode SIMPLE
  5. */
  6.  
  7. #include <windows.h>
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <malloc.h>
  12. #include <dos.h>
  13. #include <string.h>
  14. #include <conio.h>
  15.  
  16. #include "pcl4c.h"
  17. #include "use_dpmi.h"
  18.  
  19. /*** Global Variables ***/
  20.  
  21. int Port = COM1;          /* Port COM1 */
  22. int BaudCode;             /* baud rate code ( index into BaudRate[] ) */
  23. char *BaudRate[10] =  {"300","600","1200","2400","4800","9600",
  24.                        "19200","38400","57600","115200"};
  25.  
  26. /*** local prototypes */
  27.  
  28. int BaudMatch(char *);
  29. int ErrorCheck(int);
  30.  
  31. /*** main ***/
  32.  
  33. void main(int argc, char *argv[])
  34. {char c;
  35.  int  i, rc;
  36.  long hRxMem;
  37.  long hTxMem;
  38.  int  RxSelector = 0;
  39.  int  TxSelector = 0;
  40.  long Version;
  41.  if(argc!=3)
  42.    {printf("Usage: PMSIMPLE <port> <baud>\n");
  43.     exit(1);
  44.    }
  45.  /* get port number from command line */
  46.  Port = atoi(argv[1]) - 1;
  47.  if((Port<COM1) || (Port>COM20))
  48.      {printf("Port must be COM1 to COM20n");
  49.       exit(1);
  50.      }
  51.  /* get baud rate from command line */
  52.  BaudCode = BaudMatch(argv[2]);
  53.  if(BaudCode<0)
  54.      {printf("Cannot recognize baud rate = %s\n",argv[2]);
  55.       exit(1);
  56.      }
  57.  /* setup 128 byte receive buffer */
  58.  hRxMem = (long) GlobalDosAlloc( 128L );
  59.  if(hRxMem)
  60.    {/* get selector */
  61.     RxSelector = LOWORD(hRxMem);
  62.     GlobalPageLock(RxSelector);
  63.    }
  64.  else
  65.    {printf("GlobalDosAlloc failed\n");
  66.     exit(1);
  67.    }
  68.  SioRxBuf(Port,RxSelector,Size128);
  69.  printf("RX buffer allocated\n");
  70.  /* transmitter interrupt enabled ? */
  71.  if(SioInfo('I'))
  72.    {/* setup 128 byte transmit buffer */
  73.     hTxMem = (long) GlobalDosAlloc( 128L );
  74.     if(hTxMem)
  75.       {/* get selector */
  76.        TxSelector = LOWORD(hTxMem);
  77.        GlobalPageLock(TxSelector);
  78.       }
  79.     else
  80.       {printf("GlobalDosAlloc failed\n");
  81.        if(RxSelector)
  82.          {GlobalPageUnlock(RxSelector);
  83.           GlobalDosFree(RxSelector);
  84.          }
  85.        exit(1);
  86.       }
  87.     SioTxBuf(Port,TxSelector,Size128);
  88.     printf("TX buffer allocated\n");
  89.    }
  90.  /* set port parmameters */
  91.  ErrorCheck( SioParms(Port,NoParity,OneStopBit,WordLength8) );
  92.  /* reset the port */
  93.  ErrorCheck( SioReset(Port,BaudCode) );
  94.  /* set DTR and RTS */
  95.  ErrorCheck( SioDTR(Port,'S') );
  96.  ErrorCheck( SioRTS(Port,'S') );
  97.  #if 0
  98.  /* set RTS/CTS flow control */
  99.  ErrorCheck( SioFlow(Port,18) );
  100.  puts("Flow control on");
  101. #endif
  102.  /* Set FIFO level */
  103.  if(SioFIFO(Port,LEVEL_14)) printf("[16550]\n");
  104.  /* get DPMI version # */
  105.  Version = DPMI_GetVersion();
  106.  if(HIWORD(Version)) printf("\n32-bit ");
  107.  else printf("\n16-bit ");
  108.  i = LOWORD(Version);
  109.  printf("DPMI Version %d.%d\n", (i>>8),i&255);
  110.  printf("Enter terminal loop ( Type ^Z to exit )\n");
  111.  
  112.  /* enter terminal loop */
  113.  while(TRUE)
  114.      {if(SioBrkKey())
  115.         {/* restore COM port status & exit */
  116.          printf("BREAK\n");
  117.          SioDone(Port);
  118.          exit(2);
  119.         }
  120.       /* was key pressed ? */
  121.       if(kbhit())
  122.           {i = getch();
  123.            if((char)i==0x1a)
  124.               {/* restore COM port */
  125.                SioDone(Port);
  126.                /* free DOS memory */
  127.                if(RxSelector)
  128.                  {GlobalPageUnlock(RxSelector);
  129.                   GlobalDosFree(RxSelector);
  130.                  }
  131.                if(TxSelector)
  132.                  {GlobalPageUnlock(TxSelector);
  133.                   GlobalDosFree(TxSelector);
  134.                  }
  135.                exit(1);
  136.               }
  137.            else
  138.               {
  139.                SioPutc(Port,(char)i);
  140.                /*printf("(%d)",SioInfo('R') );*/
  141.               }
  142.           } /* end if */
  143.       /* any incoming over serial port ? */
  144.       i = SioGetc(Port,0);
  145.       if(i>-1) putch((char)i);
  146.       if(SioBrkSig(Port,'D')) printf("[BREAK detected]");
  147.  
  148.      } /* end while */
  149. } /* end main */
  150.  
  151. int ErrorCheck(int Code)
  152. {/* trap PCL error codes */
  153.  if(Code<0)
  154.      {printf("ERROR %d:",Code);
  155.       SioError(Code);
  156.       SioDone(Port);
  157.       exit(1);
  158.      }
  159.  return(0);
  160. } /* end ErrorCheck */
  161.  
  162.  
  163. int BaudMatch(char *P)
  164. {int i;
  165.  /* find baud rate in table */
  166.  for(i=0;i<10;i++) if(strcmp(BaudRate[i],P)==0) return(i);
  167.  return(-1);
  168. }
  169.